package com.ruyuan.eshop.seller.controller.task;

import cn.hutool.json.JSONUtil;
import com.ruyuan.eshop.common.domain.entity.AfterSaleServiceDO;
import com.ruyuan.eshop.common.service.aftersale.PaySuccessCancelOrderRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 工单自动审核的任务
 * @author pwd
 * @version 1.0
 **/
@Component
public class AfterSaleAutoAuditTask {

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(AfterSaleAutoAuditTask.class);

    @Autowired
    private PaySuccessCancelOrderRefundService paySuccessCancelOrderRefundService;

    @Scheduled(fixedRate = 5 * 1000L)
    public void execute() throws Exception {

        String queueName = "afterServiceApplyQueue";
        List<String> range = null;
        if (CollectionUtils.isEmpty(range)) {
            return;
        }

        String headTaskStr = null;

        if (StringUtils.isEmpty(headTaskStr)) {
            LOG.info("发现退款队列中的第一个任务为空 退出执行");
            return;
        }

        AfterSaleServiceDO afterSaleServiceDO = JSONUtil.toBean(headTaskStr, AfterSaleServiceDO.class);
        LOG.info("售后服务单的信息为 afterSaleServiceDO [{}]", JSONUtil.toJsonPrettyStr(afterSaleServiceDO));
        // 工单自动审核通过 自动商品入库 执行退款逻辑
        Boolean process = paySuccessCancelOrderRefundService.process(afterSaleServiceDO);
        LOG.info("处理退款的结果为 [{}]", process);
    }

}
